package com.wj.config.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wj.utils.RespUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest res, HttpServletResponse resp, Authentication authentication) throws IOException, ServletException {
        resp.setContentType("application/json;charset=UTF-8");
        String successRes = new ObjectMapper().writeValueAsString(RespUtil.success("登录成功",authentication.getName()));
        resp.getWriter().print(successRes);
    }

}
